<script setup lang="ts">
  import { ref } from 'vue'
  import { onLoad } from '@dcloudio/uni-app'
  import { addProductsOrder, playCheck, productsList } from '@/api/training'
  import { msg, wechatPayment } from '@/utils/tools'
  const bottomPadding = (uni.getSystemInfoSync().screenHeight as number) - (uni.getSystemInfoSync().safeArea.bottom as number)
  const productsId = ref(0)
  onLoad((options:any) => {
    productsId.value = Number(options.id)
    productsList({registrationId: options.id}).then(res => {
      const data = JSON.parse(JSON.stringify(res.data)).map((item:any) => {
        item.check = false
        return item
      })
      additionalProducts.value = data
    })
  })
  const additionalProducts = ref([])
  const serviceClick = (data:any) => {
    if(data.canBuy){
      data.check = !data.check
      calculateAmount()
    }else{
      msg('您已购买该服务，请前往订单查看详情')
    }
  }
  const totalPrice = ref(0)
  const calculateAmount = () => {
    totalPrice.value = 0
    additionalProducts.value.map((item:any) => {
      if(item.check){
        totalPrice.value += item.productAmount
      }
    })
    if(totalPrice.value === 0){
      totalPrice.value = 0
    }else{
      totalPrice.value = totalPrice.value * 0.01
    }
  }

  const preOrder = () => {
    const productIds = additionalProducts.value.filter((item:any)=> item.check).map((items:any) => {
      return items.id
    })
    if(productIds && productIds.length == 0){
      msg('请选择附加产品')
      return false
    }
    addProductsOrder({id:productsId.value,productIds:productIds}).then(res => {
      // 起调支付
      wechatPayment(res.data.payInfo).then((_res:any) =>{
        // 支付后检查是否支付
        playCheck({orderId:res.data.orderId}).then(resData => {
          msg('支付成功')
          const time = setTimeout(() => {
            uni.redirectTo({url:`/pageSignUp/trainOrder/trainOrder?id=${productsId.value}`})
            clearTimeout(time)
          },500)
        }).catch((err:any) => {
          msg('检查失败，请从新进行页面')
        })
      }).catch((err:any) => {
        msg('支付失败，请重试')
        uni.redirectTo({url:`/pageSignUp/productsOrder/productsOrder?id=${productsId.value}`})
      })
    }).catch(err => {
      uni.redirectTo({url:`/pageSignUp/productsOrder/productsOrder?id=${productsId.value}`})
    })
  }

</script>

<template>
  <view class="product">
    <xx-title title="附加产品"/>
    <view class="service-list" v-if="additionalProducts?.length > 0">
      <view class="service-item" @click="serviceClick(item)" v-for="(item, index) in additionalProducts" :key="index">
        <view class="service-body">
          <view class="service-title">{{ item.name }}</view>
          <view class="service-content">{{ item.remark }}</view>
        </view>
        <view class="service-money">
          ￥{{item.productAmount * 0.01 }}
        </view>
        <view class="service-btn" :class="{disable:!item.canBuy}">
          <view class="btn" :class="{check:item.check}">
            <up-icon v-if="item.check" name="checkmark-circle-fill" color="#F36E13" size="20"></up-icon>
          </view>
        </view>
      </view>
    </view>
    <view class="bottom-fixed" :style="{paddingBottom:`calc(${bottomPadding}px + 14rpx)`}">
      <view class="seizeASeat"></view>
    </view>
    <view class="bottom-btn" :style="{paddingBottom:`calc(${bottomPadding}px + 14rpx)`}">
      <view class="price">￥{{ totalPrice }}</view>
      <view class="btn group-registration" @click="preOrder">立即支付</view>
    </view>
  </view>

</template>

<style scoped lang="scss">
  .product{
    width: 100%;
    background-color: #F5F5F5;
    min-height: 100vh;
  }
  .service-list{
    margin:28rpx;
    .service-item{
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 20rpx;
      border-radius: 20rpx;
      background-color: rgba(243, 124, 43, 0.06);
      margin-bottom: 20rpx;
      &:last-child{
        margin-bottom: 0;
      }
      .service-body{
        width: calc(100% - 100px);
        .service-title{
          font-size: 28rpx;
          color: #505363;
        }
        .service-content{
          margin-top: 5rpx;
          font-size: 24rpx;
          color: #818496;
        }
      }
      .service-money{
        width: 80px;
        color: #F36E13;
      }
      .service-btn{
        width:20px;
        text-align: right;
        .btn{
          width: 20px;
          height: 20px;
          border-radius: 100rpx;
          background-color: #F5F5F5;
          border: 1rpx solid #999999;
          display: flex;
          justify-content: center;
          align-items: center;
          &.check{
            background-color: transparent;
            border: none;
          }
        }
        &.disable .btn{
          border: 1rpx solid #999999;
          background-color: #999999;
        }
      }
    }
  }
  .bottom-fixed{
    padding-top: 14px;
    background-color: #F5F5F5;
    .seizeASeat{
      height: 60rpx;
    }
  }
  .bottom-btn{
    position: fixed;
    bottom: 0;
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-left: 28rpx;
    padding-right: 28rpx;
    box-sizing: border-box;
    padding-top: 14rpx;
    background-color: #FFFFFF;
    .price{
      font-size: 40rpx;
      font-weight: bold;
      color: #F36E13;
    }
    .btn{
      padding: 16rpx 32rpx;
      margin-right:10rpx;
      text-align: center;
      border-radius: 100rpx;
      box-sizing: border-box;
      border:1rpx solid #92C75B;
      font-size: 28rpx;
      font-weight: 500;
      letter-spacing: 2rpx;
      &:last-child{
        margin-right: 0;
      }
    }
    .group-registration{
      background: #92C75B;
      color: #FFFFFF;
    }

  }
</style>
